'use strict';

angular.module('snPaas_om')
	.controller('NewMysqlCtrl',['$scope','baseUrl','versionStore','clusterStore','SpSlider','mysqlClusterStore','logStore','$q',function($scope,baseUrl,versionStore,clusterStore,SpSlider,mysqlClusterStore,logStore,$q){
		var vm = $scope;
		var params = {};
		vm.config = {
			memMin:1024,
			menInterval:1024
		};
		vm.init = function(){
			vm.basicInfo = {
				 directorUuid:'',
				 cfMysql:'',
				 deploymentName:'',
				 componentDomain:'',
				 networkId:'',
				 storageMb:1024,
				cfMysql:'',
				deployId:''
			};
			vm.deploying = false;
			vm.loading = false;
			vm.log = [];
			vm.baseUrl = baseUrl.static;
			versionStore.getList(2).then(function(data){
				vm.versions = data;
			});
			clusterStore.query().then(function(data){
				console.log(data);
				vm.cfList = data;
			});
			mysqlClusterStore.getDirectorUuid().then(function(directorUuid){
				vm.basicInfo.directorUuid = directorUuid;
			});
		};
		vm.steps = [
			{
				stepAlias: '基础配置',
				stepName: 'basicMysqlConfig'
			},
			{
				stepAlias: '组件配置',
				stepName: 'mysqlConfig'
			},
			{
				stepAlias: '完成',
				stepName: 'newSolutionDone'
			}
		];

		vm.components = [
			{
				name:'mysql'
			}
		];

	// TODO move to directive
		var stepsConfig = vm.stepsConfig = {};
        vm.stepsConfig.now = 1;
        var titleWidth = 530;
        vm.$watch('steps',function(n,o){
            var l = vm.stepsConfig.l = vm.steps.length;
            vm.stepsConfig.mr = parseInt((titleWidth -70*l)/(l-1));
        });






       	var checkName = function(name){
        	return mysqlClusterStore.checkName(name);
        };
         var getLog = function(taskId){
        	var deferred = $q.defer();
        	vm.deploying = true;
        	logStore.getLog(taskId).then(function(noti){
				deferred.resolve(noti);
        	},function(noti){
				deferred.reject(noti);
        	},function(data){
        		console.log(vm.log.length);
        		if(vm.log.length>100){
        			vm.log.shift();
        		}
        		vm.log.push(data);
        	});

			return deferred.promise;
        };

        vm.next = function(){
        	for(var i in vm.basicInfo){
        		var t = (vm.basicInfo[i]||'')+'';
        		if(t.length > 0){
        			params[i] = vm.basicInfo[i];
        		}
        		else{
        			SpSlider.slideIn({
						title:'错误',
						content:'请检查参数完整性！',
						type:'alert'
					});
        			return;
        		}
        		
        	}
        	vm.loading = true;
        	checkName(vm.basicInfo.deploymentName).then(function(res){
        		if(res){
        			vm.stepsConfig.now ++;
        		}
        		else{
        			SpSlider.slideIn({
						title:'错误',
						content:'集群名重复！',
						type:'alert'
					});
        		}
        		vm.loading = false;

        	});
        };
        vm.deploy = function(){
        	params.components = vm.components;
        	console.log(params);
        	mysqlClusterStore.deploy(params).then(function(data){
        		var mysqlClusterId = data.mysqlClusterId;
        		getLog(data.taskId).then(function(noti){
        			vm.log.push({flag:1,message:'creating servicebroker'});
        			mysqlClusterStore.createBroker(mysqlClusterId).then(function(){
        				vm.log.push({flag:1,message:'servicebroker created'});
        				SpSlider.slideIn({
							title:'操作成功',
							content:'集群'+vm.basicInfo.deploymentName+ noti,
							type:'noti'
						});
						vm.deploying = false;
						vm.stepsConfig.now ++;
						vm.doneText = '集群'+vm.basicInfo.deploymentName+ noti;
						vm.done = true;

        			},function(){
        				vm.log.push({flag:1,message:'creating servicebroker failed'});
        				vm.deploying = false;
						vm.stepsConfig.now ++;
						vm.doneText = '集群'+vm.basicInfo.deploymentName+ noti+'  servicebroker创建失败';
						vm.done = true;
        			});


					
        		},function(noti){
	        		SpSlider.slideIn({
						title:'操作失败',
						content:'集群'+vm.basicInfo.deploymentName+ noti,
						type:'alert'
					});
					vm.deploying = false;
					vm.stepsConfig.now ++;
					vm.doneText = '集群'+vm.basicInfo.deploymentName+ noti;
					vm.done = true;
        		});
        	});

        };
        vm.prev = function(){
        	$scope.stepsConfig.now --;
        };
	// end of TODO 
		vm.init();

	}]);